﻿using Pipes.Sgml;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Xml;
using Pipes.Xml;

namespace Pipes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for SgmlTextReaderTest and is intended
    ///to contain all SgmlTextReaderTest Unit Tests
    ///</summary>
  [TestClass()]
  public class SgmlTextReaderTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    public void ReadWriteTest(string expected)
    {
      ReadWriteTest(expected, expected);
    }
    public void ReadWriteTest(string start, string expected)
    {
      var reader = new Pipes.IO.StringTextSource(start).Pipe(new Pipes.Sgml.SgmlTextReader());
      var writer = new System.IO.StringWriter();
      var xml = Pipes.Sgml.HtmlTextWriter.Create(writer);
      foreach (var item in reader)
      {
        xml.Node(item);
      }
      xml.Flush();
      Assert.AreEqual(expected.Trim(), writer.ToString().Trim());
    }

    /// <summary>
    /// XMLs the read write.
    /// </summary>
    [TestMethod()]
    public void HtmlEntityReadWrite()
    {
      ReadWriteTest("<p>something &nbsp; another & &#160; &amp; &lt;-- --&gt; &copy; </p>", 
        "<p>something &nbsp; another &amp; &nbsp; &amp; &lt;-- --&gt; &#169; </p>");
    }

    [TestMethod()]
    public void HtmlFragmentReadWrite()
    {
      ReadWriteTest(@"<html><head><script type=""text/javascript"" src=""http://lumen/js/jquery-1.7.2.min.js""></script><script type=""text/javascript"" src=""http://lumen/js/jquery.ba-hashchange.min.js""></script></head><body><div style=""width: 150px; position: absolute;""><ul style=""list-style-type: none; padding: 0px""><li><a href=""#6E136DFDDE074220B8F9708787E38532"">Prepare Submission</a></li><li><a href=""#7A95B5C26C6D46D6BA534B0860DD3D4E"">Assign Team</a></li></ul></div><div style=""margin-left:150px;position:relative""><img src=""http://localhost:1487/Render.aspx?type=workflowImg&id=050F432060384A83821274A22D33AE69&db=PROD"" width=""490"" height=""218"" usemap=""map"" /><map name=""map""><area href=""#C625D4114D7545EDBD27440EF99B62F8"" shape=""rect"" coords=""0.5,118,111.6738,133.9844"" /><area href=""#1640F90DE6964FADB96A92C783426BB2"" shape=""rect"" coords=""125.5,117,198.9394,132.9844"" /></map><div style=""position:absolute;width:20px;height:20px;z-index:-1;border:1px solid blue;left:48px;top:98px;"" /></div></body></html> ",
        @"<html><head><script type=""text/javascript"" src=""http://lumen/js/jquery-1.7.2.min.js"" /><script type=""text/javascript"" src=""http://lumen/js/jquery.ba-hashchange.min.js"" /></head><body><div style=""width: 150px; position: absolute;""><ul style=""list-style-type: none; padding: 0px""><li><a href=""#6E136DFDDE074220B8F9708787E38532"">Prepare Submission</a></li><li><a href=""#7A95B5C26C6D46D6BA534B0860DD3D4E"">Assign Team</a></li></ul></div><div style=""margin-left:150px;position:relative""><img src=""http://localhost:1487/Render.aspx?type=workflowImg&id=050F432060384A83821274A22D33AE69&db=PROD"" width=""490"" height=""218"" usemap=""map"" /><map name=""map""><area href=""#C625D4114D7545EDBD27440EF99B62F8"" shape=""rect"" coords=""0.5,118,111.6738,133.9844"" /><area href=""#1640F90DE6964FADB96A92C783426BB2"" shape=""rect"" coords=""125.5,117,198.9394,132.9844"" /></map><div style=""position:absolute;width:20px;height:20px;z-index:-1;border:1px solid blue;left:48px;top:98px;"" /></div></body></html>");
    }
  }
}
